<template>
    
    <div class="login_container">
        <div class="login_box">
            <!-- 头像 -->
            <div ref="avatar" class="avatar_box">
                <img src="../assets/pic10.png" alt="">
            </div>
            <!-- 表单区域 -->
            <el-form ref="formRef" :model="form" :rules="rules" class='login_form'>
                <!-- 账号 -->
                <el-form-item prop="username">
                    <el-input v-model="form.username" prefix-icon="el-icon-user"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input v-model="form.password" type="password"
                    prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <!-- 按钮 -->
                <el-form-item class='btns'>
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button @click="resetForm">清空</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
    
</template>
<script>
export default {
    data() {
        return {
            //这是表单规则
            rules: {
                username:[
                    {required: true, message:'请输入用户名', trigger: 'blur'},
                    {min: 3, max: 6, message:'长度3~6个字符', trigger: 'blur'}
                    //trigger: blur表示在失去鼠标焦点时触发检查
                ],
                password:[
                    {required: true, message:'请输入密码', trigger: 'blur'},
                ]

            },
                
            

            // 这是登陆表单的对象
            form: {
                username: 'admin',
                password: '123456'
            }
        }
    },
    methods: {
        // 点击清空按钮，清空表单
        resetForm() {
            //console.log(this);
            this.$refs.formRef.resetFields();
        },
        login() {
            this.$refs.formRef.validate(async valid => {
                //console.log(valid);
                if(!valid) return;
                const { data : res } = await this.$http.post("login", this.form);
                console.log(res);
                if(res.meta.status !== 200) return this.$message.error("登陆失败");
                this.$message.success("登陆成功");

                //保存token
                window.sessionStorage.setItem("token", res.data.token);

                //通过导航跳转到主页
                this.$router.push('/home');
            })
        }
    }
}
</script>
<style scoped>
    
    .login_container {
        height: 100%;
        width: 100%;
        background-color: rgb(64, 78, 138);
        
        
    }
    .login_box {
        height: 300px;
        width: 450px;
        background-color: aliceblue;
        border-radius:5px;
        position:absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
    }
    .avatar_box {
        height: 130px;
        width: 130px;
        border-radius: 50%;
        position: absolute;
        padding: 10px;
        background-color: aliceblue;
        box-shadow: 0px 0px 5px #cccccc;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    .avatar_box img{
        height: 100%;
        width: 100%;
        border-radius: 50%;
    }
    .login_form {
        position:absolute;
        bottom: 0;
        padding: 25px;
        padding-bottom: 0px;
        width: 400px;
    }
    .btns {
        display: flex;
        justify-content: flex-end;
    }
</style>